<?php
/**
 * Copyright 2013 Marek Kaszewski
 *
 * This file is part of the JHUB1 Online Community Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Jhub1_Common\Entity\BO;

use Jhub1_Common\Entity\Users;
use Jhub1_Common\Entity\EndpointTypes;
use Jhub1_Common\Model\SettingsObjects\DateTimeObject;
use Jhub1_Common\Model\Key\UniqueKey;

class EndpointTypesBO extends AbstractBO
{
    private $typesPO = null;
    private $serviceLocator;

    public function __construct($serviceLocator)
    {
        $this->getEM($serviceLocator);
        $this->serviceLocator = $serviceLocator;
    }

    public function getTypesPO()
    {
        return $this->typesPO;
    }

    public function insertValue(Users $userPO, $name, $description, $className, $classInstance)
    {
        $kg = new UniqueKey($this);
        $this->em->getConnection()->beginTransaction();
        try {
            $typesPO = new EndpointTypes($userPO);
            $typesPO->setId($kg->getUUID());
            $typesPO->setName($name);
            $typesPO->setDescription($description);
            $typesPO->setClass($className);
            $typesPO->setValueParamsDef($classInstance);
            $this->em->persist($typesPO);
            $this->em->getConnection()->commit();
        } catch (Exception $e) {
            $this->em->getConnection()->rollback();
            $this->em->close();
            throw $e;
        }
        $this->em->flush();
        $this->typesPO = $typesPO;
        return $typesPO;
    }

    public function getEndpointsByUser(Users $userPO)
    {
        $typesPO = new EndpointTypes($userPO);

    }

    public function isPKUnique($primaryKey)
    {
        if(null == $this->em->getRepository('\Jhub1_Common\Entity\EndpointTypes')->find($primaryKey)) {
            return true;
        }
        return false;
    }

    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }

    public function getObjectByUUID($uuid)
    {
        return $this->em->getRepository('\Jhub1_Common\Entity\EndpointTypes')->find($uuid);
    }
}